package com.aw.guiguzi.util

import android.view.View
import android.widget.CompoundButton
import android.widget.Switch
import com.aw.baselib.util.AwToastUtil
import com.aw.guiguzi.base.MyApp
import com.aw.guiguzi.listeners.OnCommonCheckedListener
import com.aw.guiguzi.listeners.OnCommonClickListener
import com.aw.guiguzi.util.AppUserUtil.isAdmin

object CommonUtil {
    val isAllow: Boolean
        get() {
            if (!isAdmin) {
                AwToastUtil.showShort(MyApp.instance, "权限不足，不支持操作，请联系管理员")
                return false
            }
            return true
        }

    fun setOnCheckedChangeListenerNoAuth(view: Switch?, listener: OnCommonCheckedListener?) {
        view?.setOnCheckedChangeListener { buttonView: CompoundButton?, isChecked: Boolean ->
            if (listener == null) {
                return@setOnCheckedChangeListener
            }
            listener.callback(isChecked)
        }
    }

    fun setOnCheckedChangeListener(view: Switch?, listener: OnCommonCheckedListener?) {
        view?.setOnCheckedChangeListener { buttonView: CompoundButton?, isChecked: Boolean ->
            if (!isAllow) {
                view.isChecked = !isChecked
                return@setOnCheckedChangeListener
            }
            if (listener == null) {
                return@setOnCheckedChangeListener
            }
            listener.callback(isChecked)
        }
    }

    fun setOnClickListenerNoAuth(view: View?, listener: OnCommonClickListener?) {
        view?.setOnClickListener { v: View? ->
            if (listener == null) {
                return@setOnClickListener
            }
            listener.callback()
        }
    }

    fun setOnClickListener(view: View?, listener: OnCommonClickListener?) {
        view?.setOnClickListener { v: View? ->
            if (!isAllow) {
                return@setOnClickListener
            }
            if (listener == null) {
                return@setOnClickListener
            }
            listener.callback()
        }
    }
}